#include <clip.h>
#include <cmath>

// std::vector<std::tuple<Vec4, Vec4, Vec2>>
// clippingPlane(std::vector<std::tuple<Vec4, Vec4, Vec2>>&& vertList, const ClipPlane& plane) {
//     std::vector<std::tuple<Vec4, Vec4, Vec2>> outVertList;
//     for (int i = 0; i < vertList.size() - 1; ++i) {
//         Vec4 vec1 = std::get<0>(vertList[i]) - plane.point;
//         float dot1 = vec1 * plane.normal;
//         Vec4 vec2 = std::get<0>(vertList[i + 1]) - plane.point;
//         float dot2 = vec2 * plane.normal;
//         if (dot1 < 0 && dot2 < 0) {
//             continue;
//         }
//         else if(dot1 > 0 && dot2 > 0) {
//             outVertList.push_back(vertList[i + 1]);
//         }
//         else if (dot1 > 0 && dot2 < 0) {
//             float t = std::get<0>(vertList[i]).w - std::get<0>(vertList[i + 1]).w / 
//                 ((std::get<0>(vertList[i]).w - std::get<0>(vertList[i]).x) - (std::get<0>(vertList[i + 1]).w - std::get<0>(vertList[i + 1]).x));
//             
//             Vec4 intersectionPos = (std::get<0>(vertList[i + 1]) - std::get<0>(vertList[i])) * t + std::get<0>(vertList[i]);
//             Vec4 intersectionNormal = (std::get<1>(vertList[i + 1]) - std::get<1>(vertList[i])) * t + std::get<1>(vertList[i]);
//             TexCoord intersectionTexCoord = (std::get<2>(vertList[i + 1]) - std::get<2>(vertList[i])) * t + std::get<2>(vertList[i]);
//             outVertList.push_back(std::make_tuple(intersectionPos, intersectionNormal, intersectionTexCoord));
//         }
//         else if (dot1 < 0 && dot2 > 0) {
//             float t = std::get<0>(vertList[i]).w - std::get<0>(vertList[i + 1]).w / 
//                 ((std::get<0>(vertList[i]).w - std::get<0>(vertList[i]).x) - (std::get<0>(vertList[i + 1]).w - std::get<0>(vertList[i + 1]).x));
//             
//             Vec4 intersectionPos = (std::get<0>(vertList[i + 1]) - std::get<0>(vertList[i])) * t + std::get<0>(vertList[i]);
//             Vec4 intersectionNormal = (std::get<1>(vertList[i + 1]) - std::get<1>(vertList[i])) * t + std::get<1>(vertList[i]);
//             TexCoord intersectionTexCoord = (std::get<2>(vertList[i + 1]) - std::get<2>(vertList[i])) * t + std::get<2>(vertList[i]);
//             outVertList.push_back(std::make_tuple(intersectionPos, intersectionNormal, intersectionTexCoord));
//             outVertList.push_back(vertList[i + 1]);
//         }
//     }
//     Vec4 vec1 = std::get<0>(vertList[vertList.size() - 1]) - plane.point;
//     float dot1 = vec1 * plane.normal;
//     Vec4 vec2 = std::get<0>(vertList[0]) - plane.point;
//     float dot2 = vec2 * plane.normal;
// 
//     if(dot1 > 0 && dot2 > 0) {
//         outVertList.push_back(vertList[0]);
//     }
//     else if (dot1 > 0 && dot2 < 0) {
//         float t = std::get<0>(vertList[vertList.size() - 1]).w - std::get<0>(vertList[0]).w / 
//             ((std::get<0>(vertList[vertList.size() - 1]).w - std::get<0>(vertList[vertList.size() - 1]).x) - (std::get<0>(vertList[0]).w - std::get<0>(vertList[0]).x));
//         
//         Vec4 intersectionPos = (std::get<0>(vertList[0]) - std::get<0>(vertList[vertList.size() - 1])) * t + std::get<0>(vertList[vertList.size() - 1]);
//         Vec4 intersectionNormal = (std::get<1>(vertList[0]) - std::get<1>(vertList[vertList.size() - 1])) * t + std::get<1>(vertList[vertList.size() - 1]);
//         TexCoord intersectionTexCoord = (std::get<2>(vertList[0]) - std::get<2>(vertList[vertList.size() - 1])) * t + std::get<2>(vertList[vertList.size() - 1]);
//         outVertList.push_back(std::make_tuple(intersectionPos, intersectionNormal, intersectionTexCoord));
//     }
//     else if (dot1 < 0 && dot2 > 0) {
//         float t = std::get<0>(vertList[vertList.size() - 1]).w - std::get<0>(vertList[0]).w / 
//             ((std::get<0>(vertList[vertList.size() - 1]).w - std::get<0>(vertList[vertList.size() - 1]).x) - (std::get<0>(vertList[0]).w - std::get<0>(vertList[0]).x));
//         
//         Vec4 intersectionPos = (std::get<0>(vertList[0]) - std::get<0>(vertList[vertList.size() - 1])) * t + std::get<0>(vertList[vertList.size() - 1]);
//         Vec4 intersectionNormal = (std::get<1>(vertList[0]) - std::get<1>(vertList[vertList.size() - 1])) * t + std::get<1>(vertList[vertList.size() - 1]);
//         TexCoord intersectionTexCoord = (std::get<2>(vertList[0]) - std::get<2>(vertList[vertList.size() - 1])) * t + std::get<2>(vertList[vertList.size() - 1]);
//         outVertList.push_back(std::make_tuple(intersectionPos, intersectionNormal, intersectionTexCoord));
//         outVertList.push_back(vertList[0]);
//     }
// 
//     return outVertList;
// }
// 
// std::vector<std::tuple<Vec4, Vec4, Vec2>>
// SHClipping(std::vector<std::tuple<Vec4, Vec4, Vec2>>&& vertList, const Frustum& f) {
//     std::vector<std::tuple<Vec4, Vec4, Vec2>> outVertList;
//     outVertList = clippingPlane(std::move(vertList),    f.topPlane);
//     outVertList = clippingPlane(std::move(outVertList), f.bottomPlane);
//     outVertList = clippingPlane(std::move(outVertList), f.farPlane);
//     outVertList = clippingPlane(std::move(outVertList), f.nearPlane);
//     outVertList = clippingPlane(std::move(outVertList), f.leftPlane);
//     outVertList = clippingPlane(std::move(outVertList), f.rightPlane);
// 
//     return outVertList;
// }
// 